﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Globalization;
using System.Security.Principal;
using System.Diagnostics;
using System.Resources;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public partial class Program
    {
        public static string GetSelectedFileName
        {
            get
            {
                return _current.FileName;
            }
        }
        static OpenFileDialog _openDialog;
        static SaveFileDialog _saveDialog;
        static FileDialog _current;

        internal static DialogResult ShowFileDialog(
            string pFilter,
            string pInitialDirectory,
            bool pOpen,
            string pTitle)
        {
            if (pOpen)
            {
                _openDialog = new OpenFileDialog();
                _current = _openDialog;
            }
            else
            {
                _saveDialog = new SaveFileDialog();
                _current = _saveDialog;
            }
            
            _current.Filter = pFilter;
            _current.Title = pTitle;
            _current.InitialDirectory = pInitialDirectory;
            _current.CheckFileExists = pOpen;            
            return _current.ShowDialog();
        }

        static DialogResult ShowSQLFileDialog(
            string pInitialDirectory,
            bool pOpen,
            string pTitle)
        {
            return ShowFileDialog(Resources.OPEN_FILE_SQLiteFilter.
                    Replace("#",
                    Settings.Default.SQLite_DefaultFileExtension),
                pInitialDirectory,
                pOpen,
                pTitle);
        }

        static DialogResult ShowRotaFileDialog(string pInitialDirectory,
            bool pOpen,
            string pTitle)
        {
            return ShowFileDialog(Resources.OPEN_File_RotaFilter.
                    Replace("#",
                    Settings.Default.ROTAS_DefaultFileExtension),
                pInitialDirectory,
                pOpen,
                pTitle);
        }

        #region Initial directory (retrieving or ignoring default values)
        static string SQLDirectory(string pProposition)
        {
            return pProposition ?? SQLiteAccess.GetDefaultPath;
        }

        static string RotaDirectory(string pProposition)
        {
            return pProposition ?? GetDefaultPath;
        }
        #endregion

        #region public methods
        
        public static DialogResult ShowSQLSaveAsDialog(string pInitialDirectory)
        {            
            return ShowSQLFileDialog(SQLDirectory(pInitialDirectory),
                false,
                Resources.FILEDIALOGTitle_SaveAs_SQL);
        }

        public static DialogResult ShowRotaSaveAsDialog(string pInitialDirectory)
        {            
            return ShowRotaFileDialog(RotaDirectory(pInitialDirectory),
                false,
                Resources.FILEDIALOGTitle_SaveAs_RotasTemplates);
        }

        public static DialogResult ShowSQLOpenDialog(string pInitialDirectory)
        {            
            return ShowSQLFileDialog(SQLDirectory(pInitialDirectory),
                true,
                Resources.FILEDIALOGTitle_Open_SQL);
        }

        public static DialogResult ShowRotaOpenDialog(string pInitialDirectory)
        {            
            return ShowRotaFileDialog(RotaDirectory(pInitialDirectory),
                true,
                Resources.FILEDIALOGTitle_Open_RotasTemplates);
        }

        public static DialogResult ShowImportTemplatesDialog(string pInitialDirectory)
        {
            return ShowRotaFileDialog(RotaDirectory(pInitialDirectory),
                true,
                Resources.FILEDIALOGTitle_ImportTemplatesFrom);
        }
        
        #endregion
    }
}
